package com.wsc.personalblog.controller;

import com.github.pagehelper.PageInfo;
import com.wsc.personalblog.DTO.ArticleDTO;
import com.wsc.personalblog.DTO.LikeInfoDTO;
import com.wsc.personalblog.common.Result;
import com.wsc.personalblog.entity.Article;
import com.wsc.personalblog.service.ArticleService;
import com.wsc.personalblog.service.impl.LikeServiceImpl;
import com.wsc.personalblog.utils.AliYunOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api("文章相关的操作")
@RestController
@RequestMapping("/article")
@Slf4j
public class ArticleController {

    @Autowired
    ArticleService articleService;

    @Autowired
    LikeServiceImpl likeServiceImpl;

    @ApiOperation("文章点赞")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "articleId", dataType = "int", dataTypeClass = int.class,required = true, value = "文章id"),
            @ApiImplicitParam(name = "userId", dataType = "int", dataTypeClass = int.class,required = true, value = "用户id")})
    @PostMapping("/like/{articleId}/{userId}")
    public Result<String> likeArticle(
            @PathVariable("articleId") Integer articleId,
            @PathVariable("userId") Integer userId){
        String s = likeServiceImpl.likeArticle(articleId, userId);
        return Result.successHaveData(s);
    }
    @ApiOperation("取消文章点赞")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "articleId", dataType = "int", dataTypeClass = int.class,required = true, value = "文章id"),
                    @ApiImplicitParam(name = "userId", dataType = "int", dataTypeClass = int.class,required = true, value = "用户id")})
    @DeleteMapping("/unlike/{articleId}/{userId}")
    public Result<String> unlikeArticle(
            @PathVariable("articleId") Integer articleId,
            @PathVariable("userId") Integer userId){
        likeServiceImpl.unlikeArticle(articleId, userId);
        return Result.successNoData();
    }

    @ApiOperation("获取文章点赞数量")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "articleId", dataType = "int", dataTypeClass = int.class,required = true, value = "文章id"),
                    @ApiImplicitParam(name = "userId", dataType = "int", dataTypeClass = int.class,required = true, value = "用户id")})
    @PostMapping("/public/likeCount/{articleId}/{userId}")
    public Result<LikeInfoDTO> getLikeNumAndLikeStatus(
            @PathVariable("articleId") Integer articleId,
            @PathVariable("userId") Integer userId){
        LikeInfoDTO likeInfo = likeServiceImpl.getLikeNumAndLikeStatus(articleId, userId);
        return Result.successHaveData(likeInfo);
    }

    @GetMapping("/public")
    public Result<PageInfo<ArticleDTO>> getArticlePaging(
             @RequestParam(value = "categoryId" , defaultValue = "0") Integer categoryId ,
             @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum ,
             @RequestParam(value = "pageSize" , defaultValue = "100") Integer pageSize,
             @RequestParam(value = "keyword" , defaultValue = "")  String keyword){
        PageInfo<ArticleDTO> allArticlePaging = articleService.getArticlePaging(categoryId, pageNum, pageSize, keyword);
        return Result.successHaveData(allArticlePaging);
    }
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/backend")
    public Result<PageInfo<ArticleDTO>> getArticleInfo(
            @RequestParam(value = "categoryId" , defaultValue = "0") Integer categoryId ,
            @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum ,
            @RequestParam(value = "pageSize" , defaultValue = "100") Integer pageSize,
            @RequestParam(value = "keyword" , defaultValue = "")  String keyword){
        PageInfo<ArticleDTO> allArticlePaging = articleService.getArticleInfo(categoryId, pageNum, pageSize, keyword);
        return Result.successHaveData(allArticlePaging);
    }

    @PreAuthorize("hasRole('admin')")
    @DeleteMapping
    public Result<String> removeArticle(Integer articleId  , String fileName){
        articleService.removeArticle(articleId , fileName);
        return Result.successNoData();
    }

    @PreAuthorize("hasRole('admin')")
    @GetMapping("/drafts")
    public Result<Integer> getDrafts(){
        Integer DraftsCount = articleService.getDrafts();
        return Result.successHaveData(DraftsCount);
    }

    @PreAuthorize("hasRole('admin')")
    @PutMapping("/unshelve")
    public Result<String> unshelve(@RequestBody Article article) {
        articleService.unshelve(article);
        return Result.successNoData();
    }
    @PreAuthorize("hasRole('admin')")
    @PostMapping("/pub/UploadImg")
    public Result<String> uploadImg(@RequestPart("file") MultipartFile file){
        try {
            String path = articleService.uploadImg(file);
            return Result.successHaveData(path);
            }catch (IOException e){
            throw new RuntimeException("上传失败");
        }

    }

    @DeleteMapping("/public/photo")
    public Result<String> deletePhoto(@RequestParam("fileName") String fileName){
        AliYunOssUtil.DeleteOne(fileName);
            return Result.successNoData();
    }

    @PreAuthorize("hasRole('admin')")
    @PostMapping("/pub")
    public Result<String> pubArticle(@RequestBody Map<String,Object> map){
        articleService.pubArticle(map);
        return Result.successNoData();
    }

    @PreAuthorize("hasRole('admin')")
    @PostMapping("/edit")
    public Result<String> editArticle(@RequestBody Map<String,Object> map){
        articleService.editArticle(map);
        return Result.successNoData();
    }

    @PreAuthorize("hasRole('admin')")
    @GetMapping("/getArticleInfoById")
    public Result<Article> getArticleInfoById(@RequestParam("articleId")Integer articleId){
        Article article= articleService.getArticleInfoById(articleId);
        return Result.successHaveData(article);
    }

    @PreAuthorize("hasRole('admin')")
    @PutMapping("/shelve")
    public Result<String> shelve(@RequestBody Article article){
        articleService.shelve(article);
        return Result.successNoData();
    }
    @PreAuthorize("hasRole('admin')")
    @GetMapping("/draftsInfo")
    public Result<PageInfo<ArticleDTO>> getDraftsInfo(@RequestParam(value = "categoryId" , defaultValue = "0") Integer categoryId ,
                                                  @RequestParam(value = "pageNum" , defaultValue = "1") Integer pageNum ,
                                                  @RequestParam(value = "pageSize" , defaultValue = "100") Integer pageSize,
                                                  @RequestParam(value = "keyword" , defaultValue = "")  String keyword){
        PageInfo<ArticleDTO> drafts =  articleService.getDraftsInfo(categoryId,pageNum,pageSize,keyword);
        return Result.successHaveData(drafts);
    }

}
